package com.cspg.ssss.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cspg.ssss.domain.AddScoreDept;
import com.cspg.ssss.domain.CheckDept;
import com.cspg.ssss.domain.CheckItem;
import com.cspg.ssss.domain.RoleDepartEntity;
import com.cspg.ssss.service.CheckDeptService;
import com.cspg.ssss.service.CheckItemService;
import com.cspg.ssss.service.GradeRoleUserService;
import com.cspg.ssss.vo.ResultVo;
import com.cspg.ssss.vo.ResultVoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/grade")
public class GradeController {

    @Autowired
    CheckItemService checkItemService;

    @Autowired
    GradeRoleUserService gradeRoleUserService;

    @Autowired
    CheckDeptService checkDeptService;

    @GetMapping("/get-check-list")
    public ResultVo getCheckList(@RequestParam Map<String,String> req){
        List<CheckItem> retList = new ArrayList<CheckItem>();

        String isAdd = req.get("is_add");
        if(isAdd != null && isAdd.equals("1")){
            retList = checkItemService.getAddScoreCategory();
        }else {
            retList = checkItemService.getCheckCategroy();
        }
        JSONArray retArray = new JSONArray();
        if(retList != null && retList.size() > 0){
            for(int i=0;i<retList.size();i++){
                JSONObject itemObj = new JSONObject();
                itemObj.put("id",retList.get(i).getId());
                itemObj.put("item",retList.get(i).getItem());
                retArray.add(itemObj);
            }
        }

        JSONObject retObj = new JSONObject();
        retObj.put("check_list",retArray);
        ResultVo retVo = ResultVoUtil.success("success",retObj);
        return retVo;
    }

    @GetMapping("/check-item-detail")
    public ResultVo getCheckItemDetail(@RequestParam Map<String,String> req){
        String checkitemid = req.get("checkitemid");
        String is_add = req.get("is_add");
        List<CheckItem> retList = new ArrayList<CheckItem>();
        if(is_add != null && is_add.equals("1")){
            retList = checkItemService.getAddScoreCheckItemDetailTree(checkitemid);
        }else {
            retList = checkItemService.getCheckItemDetailTree(checkitemid);
        }

        JSONObject retObj = new JSONObject();
        JSONArray retArray = new JSONArray();
        if(retList != null && retList.size() > 0){
            for(int i=0;i<retList.size();i++){
                CheckItem tmpitem = retList.get(i);
                if(tmpitem.getLevel() == 2){
                    JSONObject itemObj = new JSONObject();
                    itemObj.put("id",tmpitem.getId());
                    itemObj.put("item",tmpitem.getItem());
                    JSONArray subArray = new JSONArray();
                    for(int j=0;j<retList.size();j++){
                        CheckItem subItem = retList.get(j);
                        if(subItem.getLevel() == 3 && subItem.getId().startsWith(tmpitem.getId()+".")){
                            JSONObject subitemObj = new JSONObject();
                            subitemObj.put("id",subItem.getId());
                            subitemObj.put("item",subItem.getItem());
                            subArray.add(subitemObj);
                        }
                    }
                    itemObj.put("sub_items",subArray);
                    retArray.add(itemObj);
                }
            }
        }
        retObj.put("check_detail",retArray);
        ResultVo retVo = ResultVoUtil.success("success",retObj);
        return retVo;
    }

    /**
     * 获取考核部门列表
     * @param req
     * @return
     */
    @GetMapping("/getCheckDeptList")
    public ResultVo getCheckDeptList(@RequestParam Map<String,String> req){
        String ptype = req.get("problem_type");
        String porg = req.get("depart");
        Map<String,Object> condition = new HashMap<String,Object>();
        if(ptype.equals("0")){
            condition.put("check_type",0);
            condition.put("depart_path","超高压输电公司/公司本部");
        }else {
            condition.put("check_type",1);
            //处理字符串，查出单位路径,目前只查到单位级别
            String[] arr = porg.split("/");
            String dept_path = arr[0]+"/"+arr[1];
            condition.put("depart_path",dept_path);
        }

        condition.put("depart_type",0);
        //AddScoreDept

        List<CheckDept> ls = checkDeptService.getCheckDeptByCondition(condition);
        List<AddScoreDept> retls = new ArrayList<AddScoreDept>();
        if(ls != null && ls.size()> 0){
            for(int i=0;i<ls.size();i++){
                AddScoreDept asd = new AddScoreDept();
                asd.setDepartment_id(ls.get(i).getDepart_id());
                asd.setDepartment_path(ls.get(i).getDepart_path());
                retls.add(asd);
            }
        }

        ResultVo retVo = ResultVoUtil.success("success",retls);

        return retVo;

    }
}
